﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Modify;
using AssetsSys.model;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Assets;
using AssetsSys.WebApi.Models.Auth;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 固定资产
    /// </summary>
    [MyAuth]
    //可以跨域
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/assets")]
    public class AssetsController : ApiController
    {
        /// <summary>
        /// 获取所有资产
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public async Task<IHttpActionResult> GetAssetsList([FromUri]AssetsQuertDto request)
        {
            var data = await AssetsManger.GetAssetsList(request);
            return this.SendData(data);
        }

        /// <summary>
        /// 添加资产
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("info")]
        public async Task<IHttpActionResult> CreateAssets(CreateAssetsViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            var type = await TypeManger.GetById(model.TypeId);
            var user = ((UserIdentity)User.Identity).Id;
            model.StorageUserId = user;
            var isSucess = await AssetsManger.CreateAssets(model.AssetsName,
                model.AssetsDescribe,
                model.Decimal,
                model.SourceId,
                model.StatusId,
                model.TypeId,
                type.Code,
                model.PrincipalUserId,
                user);
            return isSucess ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }

        /// <summary>
        /// 获取资产信息
        /// </summary>
        /// <param name="assetsid">资产Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{assetsid}/info")]
        public async Task<IHttpActionResult> GetAssetsInfo(Guid assetsid)
        {
            var model = await AssetsManger.GetById(assetsid);
            return this.SendData(model);
        }

        /// <summary>
        /// 修改资产信息
        /// </summary>
        /// <param name="assetsid">资产Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{assetsid}/info")]
        public async Task<IHttpActionResult> UpdateAssetsInfo(Guid assetsid, AssetsModel model)
        {
            var userId = ((UserIdentity) User.Identity).Id;
            model.Id = assetsid;
            var assets =await AssetsManger.GetById(assetsid);
            if (assets.AssetsName == model.AssetsName
                && assets.AssetsDescribe == model.AssetsDescribe
                && assets.Decimal == model.Decimal)
            {
                return this.ErrorData("数据没有改动");
            }
            var modifyStr = "变动了一些数据：";
            modifyStr = modifyStr + (assets.AssetsName == model.AssetsName ? "" : "资产名称-"+assets.AssetsName + "->"+ model.AssetsName+";");
            modifyStr = modifyStr + (assets.AssetsDescribe == model.AssetsDescribe ? "" : "资产描述-" + assets.AssetsDescribe + "->" + model.AssetsDescribe + ";");
            modifyStr = modifyStr + (assets.Decimal == model.Decimal? "" : "资产价值-" + assets.Decimal + "->" + model.Decimal + ";");
            await ModifyManger.AddModify(assetsid, assets.StatusId, modifyStr, userId, userId, 0);
            return await AssetsManger.Update(model) ? this.SendData("修改成功") : this.ErrorData("修改失败");
        }

        /// <summary>
        /// 获取资产统计信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("countinfo")]
        public async Task<IHttpActionResult> GetCountInfo()
        {
            var data = await AssetsManger.GetCountInfo();
            return this.SendData(data);
        }

    }
}
